package ${pPackage}.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
 * ${table.comment}
 */
@RestController
@RequestMapping(value="/${ClassName}")
public class  ${ClassName}Controller {
    @Autowired
    private I${ClassName}Service ${ClassName}Service;


    /**
     * 分页列表查询
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(${ClassName}Query ${className},
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        QueryWrapper<${ClassName}DO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(${ClassName}DO::getCreateTime);
        Page<${ClassName}DO> page = new Page<>(pageNo, pageSize);
        IPage<${ClassName}DO> pageList = ${ClassName}Service.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *   添加
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ${ClassName}DO ${className}) {
        ${ClassName}Service.save(${className});
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     */
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ${ClassName}DO ${className}) {
        ${ClassName}Service.updateById(${className});
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        ${ClassName}Service.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        ${ClassName}Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ${ClassName}DO ${className} =  ${ClassName}Service.getById(id);
        if(${className}==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(${className});
    }


}
